function ajaxFileUpload(fileFieldId, url, arrSettings) {
  url = url || W_AJAX + 'common/upload_temp_avatar/';
	var divWidth = 68 || arrSettings.width;
	var divHeight = 68 || arrSettings.height;
  
	var hiddenBufferId = fileFieldId + '_file';
	var previewBlockId = fileFieldId + '_upload';
	var linkToDeleteId = fileFieldId + '_delete';
  /*
	$("#temp").ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
	*/
	
	$.ajaxFileUpload({
			url: url,
			secureuri: false,
			fileElementId: fileFieldId,
			dataType: 'json',
			success: function (data, status) {
				if(typeof(data.error) != 'undefined') {
					if(data.error != '') {
						alert(data.error);
					}
					else	{
						$('#' + hiddenBufferId).val(data.pic_file);
						var imageSrc = W_IMAGES + 'essence/user/tmp/s_' + data.pic_file;
            var imgWidth  = parseInt(data.pic_width);
            var imgHeight  = parseInt(data.pic_width);
            var vertMargin = parseInt((divHeight - imgHeight) / 2);
            var horMargin = parseInt((divWidth - imgWidth) / 2);
						$('#' + previewBlockId).find('img').hide().attr('src', imageSrc).css({ "width": imgWidth + "px", "height": imgHeight + "px", "margin":  vertMargin + "px " + horMargin + "px" }).show();
						var strDeleteLink = '<a href="javascript:emptyAvatar(\'' + data.pic_file + '\', \'' + fileFieldId + '\');">delete</a>';
						
						$('#' + linkToDeleteId).html(strDeleteLink);
					}
				}
			},
			error: function (data, status, e)	{
				alert(e);
			}
	});
	return false;
}

function emptyAvatar(avatarFileName, fileFieldId) {
  var defaultImg = '<img style="width: 30px; height: 30px; margin: 19px;" class="borderUserAvatarSmall" alt="" src="/topclient/content/images/default/s_user.gif">';
  var urlToDelete = W_AJAX + 'common/delete_temp_avatar/';
  
  var previewBlockId = fileFieldId + '_upload';
	var hiddenBufferId = fileFieldId + '_file';
  var filePathContainerId = fileFieldId + '_show_path';
  var linkToDeleteId = fileFieldId + '_delete';
  $.post(urlToDelete,
    {
      avatar: avatarFileName
    },
    function(responseText) {
   	  if (navigator.userAgent.indexOf ("Opera") != -1) var arrInfo = eval('(' + responseText + ')');
    	else var arrInfo = JSON.parse(responseText);
      if (arrInfo['result'] == 'OK') {
				$('#' + previewBlockId).find('img').replaceWith(defaultImg);
				$('#' + hiddenBufferId).empty();
				$('#' + linkToDeleteId).empty();
				$('#' + filePathContainerId).empty();
      }
      else alert(arrInfo['err_text']);
		}
	);
}

